#include "GY_ImagStateToolButton.h"

GY_ImagStateToolButton::GY_ImagStateToolButton(QWidget *parent)
    :GY_StateToolButton(parent)
{
    connect(this,&GY_StateToolButton::sendButtonStateChanged,this,&GY_ImagStateToolButton::rcvImagIconCganged);
}

/**
 * @brief setButtonIcon 设置按钮名称和图标
 * @param iconNormal 原始图标
 * @param iconPress 按下后的图标
 * @param iconHover 鼠标停留时的图标
 */
void GY_ImagStateToolButton::setButtonIcon(const QString &iconNormal, const QString &iconHover, const QString &iconPress)
{
    if(!iconNormal.isNull())
    {
        m_normal_icon = iconNormal;
        m_hover_icon = iconHover;
        m_click_icon = iconPress;
        if(iconHover.isNull())
        {
            m_hover_icon = iconNormal;
        }
        if(iconPress.isNull())
        {
            m_click_icon = iconNormal;
        }
        setIcon(QIcon(m_normal_icon));//设置图标
    }
}

void GY_ImagStateToolButton::setImagIcon(const QString &iconNormal)
{
    this->setIcon(QIcon(iconNormal)); //设置为正常图标
}

/**
 * @brief enterEvent 鼠标悬停时
 * @param event 悬停事件
 */
void GY_ImagStateToolButton::enterEvent(QEvent *event)
{
    setIcon(QIcon(m_hover_icon));
    QToolButton::enterEvent(event);
}

/**
 * @brief leaveEvent 鼠标离开时
 * @param event 离开时间
 */
void GY_ImagStateToolButton::leaveEvent(QEvent *event)
{
    if(isChecked()) //如果当前按钮被勾选了
    {
        setIcon(QIcon(m_click_icon));
    }
    else
    {
        setIcon(QIcon(m_normal_icon));
    }
    QToolButton::leaveEvent(event);
}

/**
 * @brief mousePressEvent 鼠标点击时
 * @param event 点击事件
 */
void GY_ImagStateToolButton::mousePressEvent(QMouseEvent *event)
{
    if (Qt::LeftButton == event->button())
    {
        setIcon(QIcon(m_click_icon));
    }
    QToolButton::mousePressEvent(event);
}

void GY_ImagStateToolButton::rcvImagIconCganged(GY_StateToolButton *pbutton)
{
    pbutton->setIcon(QIcon(m_normal_icon));
}

